<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\AdminUser;
use think\Session;
class Login extends Controller
{
	public function login()
	{

		$data = input('post.');
		$rows = Db::name('admin_user')->where(['admin_username'=>$data['admin_username'],'admin_pwd'=>md5($data['admin_pwd']),'admin_status'=>'1'])->find();
		if($rows){
			Session::set('admin_username',$data['admin_username']);
			$arr = ['status'=>'1','message'=>'登陆成功'];
		}else{
			$arr = ['status'=>'0','message'=>'登录失败'];
		}
		return  json($arr);
	}

	public  function nodeSession()
	{
        $username = Session::get('admin_username');
        $admin_id = Db::name('admin_user')->field('admin_id')->where(['admin_username'=>$username])->find();
        // dump($admin_id);die;
        $role_id = Db::name('admin_role')->field('rid')->where('aid',$admin_id['admin_id'])->select();

        //	二维数组转成一维数组
        $result = array_reduce($role_id, function ($result, $value) {
		    return array_merge($result, array_values($value));
		}, array());
        // dump($result);die;


		//根据rid 查询nid
        $nid = Db::name('role_node')->field('nid')->where('rid','in',$result)->select();

        
         $res = array_reduce($nid, function ($result, $value) {
		    return array_merge($result, array_values($value));
		}, array());


         // 去处重复nid
        $newnid = array_unique($res);

        $cm =  Db::name('node')->field('node_cname,node_mname')->where('node_id','in',$newnid)->select();
        foreach($cm as $v){
        	$controller = ucfirst($v['node_cname']);
        	$method = strtolower($v['node_mname']); 
        	$cjoinm[] = $controller.$method;
        }
        Session::set('cjoinm',$cjoinm);
	}


	//退出登录
	public function exitLogin()
	{
		Session::delete('admin_username');
		$this->redirect('admin/index/index');
	}
}